package com.example.smartcity.ui.dashboard;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.smartcity.R;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class DashboardFragment extends Fragment {


    private ListView functionList;
    private Intent intent = null;
    FragmentFunction fragmentFunction=null;
    Bundle bundle;
    private String[] functionMenu = new String[]{"生活服务","便民服务","车主服务"};
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        functionList = root.findViewById(R.id.functionList);
        initView();
        return root;
    }
    public void initView(){
        bundle = new Bundle();
        loadFirst(0);
        functionList.setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,functionMenu));
        //
        functionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                            loadFirst(position);
                        break;
                    case 1:
                        String[] serverT = {"生活缴费"};
                        int[] serverI = {R.drawable.lifegit};
                        bundle.putString("functionMenu",functionMenu[position]);
                        bundle.putStringArray("title",serverT);
                        bundle.putIntArray("icon",serverI);
                        break;
                    case 2:
                        String[] CarServerT = {"违章查询","停车场"};
                        int[] CarServer = {R.drawable.findcar,R.drawable.citysubway};
                        bundle.putString("functionMenu",functionMenu[position]);
                        bundle.putStringArray("title",CarServerT);
                        bundle.putIntArray("icon",CarServer);
                        break;
                }
                fragmentFunction = new FragmentFunction();
                fragmentFunction.setArguments(bundle);
                ChangeFragment(fragmentFunction);
            }
        });
    }

    public void ChangeFragment(Fragment fragment){
        FragmentManager manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.functionLayout,fragment);
        transaction.commit();
    }
    public void loadFirst(int position){
        String[] lifeT = {"城市地铁","智慧巴士","门诊预约"};
        int[] lifeI = {R.drawable.citysubway,R.drawable.smartbar,R.drawable.hospital};
        bundle.putString("functionMenu",functionMenu[position]);
        bundle.putStringArray("title",lifeT);
        bundle.putIntArray("icon",lifeI);
        fragmentFunction = new FragmentFunction();
        fragmentFunction.setArguments(bundle);
        ChangeFragment(fragmentFunction);
    }
}